#ifndef WRAP_HPP
#define WRAP_HPP
#include <iostream>
#include "unistd.h"
#include "arpa/inet.h"

void PerrExit(const char *s)
{
    perror(s);
    exit(-1);
}
int Socket(int family, int type, int protocol)
{
    int n;
    if ((n = socket(family, type, protocol)) < 0)
        PerrExit("socket error");
    return n;
}
int Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
    int n;
    n = connect(fd, sa, salen);
    if (n < 0)
        PerrExit("connect error");
    return n;
}
int Close(int fd)
{
    int n;
    if ((n = close(fd)) < 0)
        PerrExit("close error");
    return n;
}
ssize_t Read(int fd, char *ptr, size_t nbyte)
{
    ssize_t n;
    do
    {
        n = read(fd, ptr, nbyte);
        if (n >= 0)
            return n;
    } while (errno == EINTR);
    PerrExit("read error");
    return -1;
}
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
    ssize_t n;
    do
    {
        n = write(fd, ptr, nbytes);
        if (n >= 0)
            return n;
    } while (errno == EINTR);
    PerrExit("write error");
    return -1;
}
void Sleep_ms(int n){
    struct timeval delay;
    delay.tv_sec = 0;
    delay.tv_usec = n * 1000;
    select(0, NULL, NULL, NULL, &delay);
}

#endif /* WRAP_HPP */
